package com.scvtc.service.comprehensive.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.comprehensive.ComprehensivePolicyDocumentDto;
import com.scvtc.domain.entity.comprehensive.ComprehensivePolicyDocument;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.mapper.comprehensive.ComprehensivePolicyDocumentMapper;
import com.scvtc.service.comprehensive.ComprehensivePolicyDocumentService;
import com.scvtc.utils.BeanCopyUtils;
import com.scvtc.utils.LikeQueryUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 综合模块(ComprehensivePolicyDocument)表服务实现类
 *
 * @author yiFei
 * @since 2022-04-13 17:38:27
 */
@Service("comprehensivePolicyDocumentService")
public class ComprehensivePolicyDocumentServiceImpl extends ServiceImpl<ComprehensivePolicyDocumentMapper, ComprehensivePolicyDocument> implements ComprehensivePolicyDocumentService {

    /**
     * 查询所有政策文件
     *
     * @param current
     * @param size
     * @param comprehensivePolicyDocumentDto
     * @return
     */
    @Override
    public ResResult<PageVo<List<ComprehensivePolicyDocumentDto>>> getAllPolicyDocument(Long current, Long size, ComprehensivePolicyDocumentDto comprehensivePolicyDocumentDto) {
        // 查询学生信息
        Page<ComprehensivePolicyDocument> page = page(
                new Page<>(current, size),
                LikeQueryUtil.likeQuery(comprehensivePolicyDocumentDto, ComprehensivePolicyDocument.class)
        );
        // 封装 Bean
        List<ComprehensivePolicyDocument> records = page.getRecords();
        List<ComprehensivePolicyDocumentDto> comprehensivePolicyDocumentDtoList = BeanCopyUtils.copyBean(records, ComprehensivePolicyDocumentDto.class);
        long total = page.getTotal();
        // 统一返回
        return ResResult.noMsgSuccess(new PageVo<>(comprehensivePolicyDocumentDtoList, total));
    }

    /**
     * 添加单个政策文件
     *
     * @param comprehensivePolicyDocumentDto
     * @return
     */
    @Override
    public ResResult<?> addPolicyDocument(ComprehensivePolicyDocumentDto comprehensivePolicyDocumentDto) {
        ComprehensivePolicyDocument comprehensivePolicyDocument = BeanCopyUtils.copyBean(comprehensivePolicyDocumentDto, ComprehensivePolicyDocument.class).setId(null);
        if (save(comprehensivePolicyDocument)) {
            return ResResult.success(comprehensivePolicyDocument.getId());
        }
        return ResResult.fail();
    }

    /**
     * 修改单个政策文件
     *
     * @param comprehensivePolicyDocumentDto
     * @return
     */
    @Override
    public ResResult<?> updatePolicyDocument(ComprehensivePolicyDocumentDto comprehensivePolicyDocumentDto) {
        ComprehensivePolicyDocument comprehensivePolicyDocument = BeanCopyUtils.copyBean(comprehensivePolicyDocumentDto, ComprehensivePolicyDocument.class);
        if (updateById(comprehensivePolicyDocument)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除单个政策文件
     *
     * @param id
     * @return
     */
    @Override
    public ResResult<?> deletePolicyDocument(Long id) {
        if (removeById(id)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除多个政策文件
     *
     * @param ids
     * @return
     */
    @Override
    public ResResult<?> deleteMultiplePolicyDocument(Long[] ids) {
        if (removeByIds(Arrays.asList(ids))) {
            return ResResult.success();
        }
        return ResResult.fail();
    }
}

